package bat.iog.hytool.reactor.multiple;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

class MultiReadHandler {

    private SelectionKey selectionKey;

    public MultiReadHandler(SelectionKey selectionKey) {
        this.selectionKey = selectionKey;
    }
    
    public void run() {
        try {
            final SocketChannel sc = (SocketChannel) selectionKey.channel();
            //使用线程池，异步处理读请求
            ThreadPool.getPool().submit(new Runnable() {
                @Override
                public void run() {
                    doRead(sc);
                }
            });

            //处理完读请求，将通道注册为写
            Selector selector = selectionKey.selector();
            SelectionKey sk = sc.register(selector, SelectionKey.OP_WRITE);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void doRead(SocketChannel ssc) {
        System.out.println("读取数据，然后做一些数据处理");
    }
}
